package com.zy.smbms.controller;

import com.zy.smbms.pojo.SmbmsProvider;
import com.zy.smbms.service.SmbmsProviderService;
import com.zy.smbms.utils.PageSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("providerController")
public class ProviderController
{
    @Autowired
    private SmbmsProviderService smbmsProviderService;

    //供应商
    @RequestMapping("providerlist")
    public String ProviderList(HttpSession session, String proname, String pageNo)
    {
        SmbmsProvider provider=new SmbmsProvider();
        if("".equals(proname))
            proname=null;
        provider.setProname(proname);
        Map<String,Object> map=new HashMap<>();
        int pageNon,pageSize,totalPageCount;
        if(pageNo==null)
        {
            pageNo="1";
        }
        pageNon=Integer.valueOf(pageNo);
        pageSize=3;
        PageSupport page=new PageSupport();
        page.setCurrentPageNo(pageNon);
        int count=smbmsProviderService.selectTotalByProName(provider);
        page.setPageSize(pageSize);
        page.setTotalCount(count);
        totalPageCount=page.getTotalPageCount();
        if(pageNon<1)
            pageNon=1;
        if(pageNon>totalPageCount)
            pageNon=totalPageCount;
        int[] num={(pageNon-1)*pageSize,pageSize};
        map.put("proname",provider.getProname());
        map.put("num",num);
        List<SmbmsProvider> smbmsProviders = smbmsProviderService.selectProviderListByName(map);
        session.setAttribute("providerList",smbmsProviders);
        session.setAttribute("proname",provider.getProname());
        session.setAttribute("page2",page);
        return "providerList";
    }
    @RequestMapping("ProviderView")
    public String ProviderView()
    {
        return "providerView";
    }

    @RequestMapping("queryProvider")
    @ResponseBody
    public String selectProviderById(HttpSession session,String id)
    {
        SmbmsProvider provider=smbmsProviderService.selectProviderById(Long.parseLong(id));
        session.setAttribute("providers",provider);
        return "OK";
    }

    //更新供应商信息
    @RequestMapping("ProviderUpdate")
    public String ProviderUpdate()
    {
        return "providerUpdate";
    }

    @RequestMapping("providerUpdate")
    @ResponseBody
    public String updateProvider(SmbmsProvider provider)
    {
        System.out.println("Id:"+provider.getId());
        System.out.println("编号"+provider.getProcode());
        int i = smbmsProviderService.updateProviderById(provider);
        if(i>0)
            return "OK";
        else
            return "False";
    }

    @RequestMapping("deleteProviderById")
    @ResponseBody
    public String deleteProviderById(String id)
    {
        int i=smbmsProviderService.deleteProviderById(Long.valueOf(id));
        if(i>0)
            return "OK";
        else
            return "False";
    }

    @RequestMapping("addProvider")
    public String addProvider()
    {
        return "providerAdd";
    }

    @RequestMapping("AddProvider")
    @ResponseBody
    public String AddProvider(SmbmsProvider provider)
    {
        Date date=new Date();
        provider.setCreationdate(date);
        System.out.println(provider);
        int i=smbmsProviderService.addProvider(provider);
        if(i>0)
            return "OK";
        else
            return "False";
    }
}
